package IBBB;

import IBBB.model.UserMessage;
import IBBB.model.WordStockMessage;
import IBBB.view.BeginLearn;
import IBBB.view.Login;
import IBBB.view.RootPane;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import java.io.IOException;
import java.util.*;

public class Main extends Application {
    UserMessage userMessage;
    WordStockMessage wordStockMessage;
    private void getMessage() {
        Map<String, List<Integer>> info = new LinkedHashMap<>();
        List<Integer> schAndPl = new LinkedList<>();
        schAndPl.add(15);
        schAndPl.add(25);
        List<Integer> schAndPl2 = new LinkedList<>();
        schAndPl2.add(268);
        schAndPl2.add(30);
        info.put("CET4", schAndPl);
        info.put("CET6", schAndPl2);
        userMessage = new UserMessage("zhangxiaozheng", info);

        Map<String, Integer> words = new HashMap<>();
        words.put("CET4", 1386);
        words.put("CET6", 2687);
        words.put("KaoYan", 3699);
        wordStockMessage = new WordStockMessage(words);
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        getMessage();
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("view/RootPane.fxml"));
        Parent root = loader.load();
        RootPane rootPane = (RootPane) loader.getController();
        primaryStage.setTitle("IBBB");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.getIcons().add(new Image("IBBB.png"));

        FXMLLoader loader1 = new FXMLLoader();
        loader1.setLocation(getClass().getResource("view/Login.fxml"));
        Parent login = loader1.load();
        Login loginController = loader1.getController();
        loginController.onSuccessProperty().addListener(((observableValue, s, t1) -> {
            if (t1.equals("success")) {
                try {
                    rootPane.init(loginController.getUserMessage(), loginController.getWordStockMessage());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                primaryStage.show();
            }
        }));
        Stage loginStage = new Stage(StageStyle.UTILITY);
        loginController.init(loginStage);
        loginStage.setTitle("登录");
        loginStage.setScene(new Scene(login, 400, 210));
        loginStage.show();
        loginStage.setOnCloseRequest(windowEvent -> primaryStage.close());
        //primaryStage.show();

        //loginStage.close();
        // Test Code

        for (Map.Entry<StringProperty, List<IntegerProperty>> item: userMessage.getWordStockInfo().entrySet()) {
            List<IntegerProperty> temp = new LinkedList<IntegerProperty>();
            temp.add(new SimpleIntegerProperty(500));
            temp.add(new SimpleIntegerProperty(15));
            item.setValue(temp);
            break;
        }
    }


    public static void main(String[] args) {
        launch(args);
    }
}
